package com.ylsd.prospectivestudent.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSON;
import com.ylsd.prospectivestudent.entity.*;
import com.ylsd.prospectivestudent.service.PSService;
import com.ylsd.prospectivestudent.utils.BeanCopyUtils;
import com.ylsd.prospectivestudent.utils.EdustudentListener;
import com.ylsd.prospectivestudent.utils.WebUtils;
import jdk.management.resource.ResourceRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.ylsdxy.model.response.CustomPage;
import org.ylsdxy.model.response.ResponseResult;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/*
* 意向学员
* */
@RestController
@RequestMapping("/prospectiveStudent")
public class PSController {
    @Resource
    private PSService psService;
    @Resource
    EdustudentListener edustudentListener;

    @PostMapping("/findByPSPage")
    public ResponseResult<CustomPage<List<ProspectiveStudent>>> findByPage(@RequestBody ProspectiveStudent prospectiveStudent){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",psService.findByPage(prospectiveStudent));
    }

    @PostMapping("/insertPS")
    public ResponseResult<Boolean> insertPS(@RequestBody ProspectiveStudent student){
        boolean result = psService.insertPS(student);
        if (result){
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }else {
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"error",null);
        }
    }

    @PostMapping("/delPS")
    public ResponseResult<Boolean> delPS(@RequestBody List<Integer> id){
        boolean result = psService.delPS(id);
        if (result){
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }else {
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"error",null);
        }
    }

    @PostMapping("/updatePS")
    public ResponseResult<Boolean> updatePS(@RequestBody ProspectiveStudent student){
        boolean result = psService.updatePS(student);
        if (result){
            return new ResponseResult<>(ResponseResult.SUCCESS,"success",true);
        }else {
            return new ResponseResult<>(ResponseResult.SYSTEM_ERROR,"error",null);
        }
    }

    //删除记录
    @PostMapping("/findByDelPage")
    public ResponseResult<CustomPage<List<ProspectiveStudent>>> findByDelPage(@RequestBody ProspectiveStudent prospectiveStudent){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",psService.findByDelPage(prospectiveStudent));
    }

    //通过招生来源查询
    @PostMapping("/findBySource")
    public ResponseResult<List<StudentSource>> findBySource(){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",psService.findBySource());
    }

    @GetMapping("/findByTeacher")
    public ResponseResult<List<Teacher>> findByTeacher(){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",psService.findByTeacher());
    }

    @PostMapping("/findByCid/{cid}")
    public ResponseResult<ProspectiveStudent> findByCid(@PathVariable Integer cid){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",psService.findByCid(cid));
    }

    @PostMapping("/findDelDetailById/{id}")
    public ResponseResult<ProspectiveStudent> findDelDetailById(@PathVariable Integer id){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",psService.findDelDetailById(id));
    }


    @GetMapping("/export")
    public void exportUser(HttpServletResponse response){
        try {
            WebUtils.setDownLoadHeader("学生名单.xlsx",response);
            //获取需要导出的数据
            List<PSExcel> userList = psService.findByExcel();
            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), PSExcel.class).autoCloseStream(Boolean.FALSE).sheet("导出")
                    .doWrite(userList);
        } catch (Exception e) {
            ResponseResult result = new ResponseResult(444,"导出错误",null);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }
    @GetMapping("/exportModel")
    public void exportModel(HttpServletResponse response){
        try {
            WebUtils.setDownLoadHeader("学生名单.xlsx",response);
            //获取需要导出的数据
            List<PSExcelModel> userList = psService.findByExcelModel();
            //把数据写入到Excel中
            EasyExcel.write(response.getOutputStream(), PSExcelModel.class).autoCloseStream(Boolean.FALSE).sheet("导出")
                    .doWrite(userList);
        } catch (Exception e) {
            ResponseResult result = new ResponseResult(444,"导出错误",null);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }

    @PostMapping("/importStudents")
    public ResponseResult<Boolean> importStudents(@RequestParam(value = "file") MultipartFile multipartFile) {
        try {
            ExcelReader reader = EasyExcel.read(multipartFile.getInputStream(), ImportStudents.class, edustudentListener).build();
            ReadSheet sheet = EasyExcel.readSheet(0).build();
            reader.read(sheet);
            reader.finish();
        } catch (IOException e) {
            return new ResponseResult<>(500, "系统发生未知错误，打电话让运维来加班！", null);
        }
        return new ResponseResult<>(ResponseResult.SUCCESS, "success", null);
    }

    @GetMapping("/findCountGroupByDate")
    public ResponseResult<List<CountGroup>> findCountGroupByDate(){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",psService.findCountGroupByDate());
    }

    @GetMapping("/findPie")
    public ResponseResult<List<PSPie>> findPie(){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",psService.findPie());
    }

    @GetMapping("/piecount")
    public ResponseResult<Integer> piecount(){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",psService.piecount());
    }

    @GetMapping("/newStudent")
    public ResponseResult<Integer> newStudent(){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",psService.newStudent());
    }

    @GetMapping("/delStudent")
    public ResponseResult<Integer> delStudent(){
        return new ResponseResult<>(ResponseResult.SUCCESS,"success",psService.delStudent());
    }
}
